﻿using System;
using System.Collections.Generic;
using System.IO.IsolatedStorage;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using Microsoft.Devices;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;

namespace power
{
    public partial class takepincture : PhoneApplicationPage
    {

        PhotoCamera _cam;
        VideoBrush _videoBrush = new VideoBrush();

        public takepincture()
        {
            InitializeComponent();
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            
            _cam = new PhotoCamera();

            _cam.Initialized += new EventHandler<CameraOperationCompletedEventArgs>(cam_Initialized);
            _cam.AutoFocusCompleted += new EventHandler<CameraOperationCompletedEventArgs>(cam_AutoFocusCompleted);
            videoRec.Fill = _videoBrush;
            _videoBrush.SetSource(_cam);

            base.OnNavigatedTo(e);
        }

        void cam_Initialized(object sender, CameraOperationCompletedEventArgs e)
        {
            _cam.FlashMode = FlashMode.Auto;
            _cam.Focus();
        }

        void cam_AutoFocusCompleted(object sender, CameraOperationCompletedEventArgs e)
        {

            WriteableBitmap Foto = new WriteableBitmap(480,735);
            Foto.Render(videoRec, null);
            Foto.Invalidate();
            var isf = IsolatedStorageFile.GetUserStoreForApplication();
            if (!isf.DirectoryExists("/Shared/ShellContent"))
            {
                isf.CreateDirectory("/Shared/ShellContent");
            }

            using (var stream = isf.OpenFile("/Shared/ShellContent/Foto.jpg", System.IO.FileMode.OpenOrCreate))
            {
                Foto.SaveJpeg(stream, Foto.PixelWidth, Foto.PixelHeight, 0, 100);
                stream.Close();
                stream.Dispose();
               
            
            }
            isf.Dispose();
        }


         //  new Uri("isostore:/Shared/ShellContent/Foto.jpg", UriKind.Absolute);




        private void Path_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {

        }
    }
}